<?php

/**
 * 菜单管理
 *
 * @author suning <snsnsky@gmail.com>
 */

use \Common\Bridge;

define("SITE_AUTH", 'admin-menu');

require '../../../common.inc.php';

class menu extends lib\core\AdminAction
{

    public function doDefault()
    {
        $menu = \Admin\Menu::getAllMenu();
        $twomenu = [];
        $onemenu = $twomenu[0] = "<option value='0'>主菜单</option>";

        foreach ($menu as $v)
        {
            $id = $v['AdminMenuId'];
            $onemenu .= "<option value='$id'>-- ".$v['Name']."</option>";
            $twomenu[$id] = "<option value='$id'>".$v['Name']."</option>";

            $child = isset($v['child']) ? $v['child'] : [];
            foreach ($child as $value)
            {
                $cid = $value['AdminMenuId'];
                $twomenu[$id] .= "<option value='$cid'>-- ".$value['Name']."</option>";
            }
        }
        $page = $this->app->page();
        $page->value("menu", $menu);
        $page->value("onemenu", $onemenu);
        $page->value("twomenu", json_encode($twomenu));

        $page->output('admin/menu.tpl');
    }

    public function doAddMenu()
    {
        $dbr = Bridge::pdo(Bridge::DB_ADMIN_R);
        $dbw = Bridge::pdo(Bridge::DB_ADMIN_W);

        $new['Name'] = strval($_POST['name']);
        $new['Key'] = strval($_POST['key']);
        $new['Url'] = strval($_POST['url']);
        $new['ParentId'] = intval($_POST['parent']);
        $new['DisplayOrder'] = intval($_POST['order']);
        $id = intval($_POST['id']);

        if ($new['ParentId'] != 0)
        {
            $parent_menu = $dbr->query("select * from AdminMenu where AdminMenuId=".$new['ParentId'])->fetch();
            if (empty($parent_menu))
            {
                self::output(['title'=>'上级菜单不存在','type'=>'error']);
            }
        }

        if ($id == 0)
        {
            //添加
            $key_check = $dbr->query("select * from AdminMenu where `Key`='{$new['Key']}'")->fetch();
            if (!empty($key_check))
            {
                self::output(['title'=>'此KEY已存在','type'=>'error']);
            }
            //同名菜单验证
            $menu_check = $dbr->query("select * from AdminMenu where `Name`='{$new['Name']}' and ParentId={$new['ParentId']}")->fetch();
            if (!empty($menu_check))
            {
                self::output(['title'=>'同级菜单下已存在此菜单','type'=>'error']);
            }
            $re = $dbw->insert('AdminMenu', $new);
            $this->adminLog('添加菜单', $dbw->lastInsertId(), 'add', serialize($new));

            //将此菜单权限加入当前登录管理员所在的权限组
            $group = \Admin\AdminAuth::getGroupById($_SESSION['admin_info']['AdminUserGroup']);
            $group_auth = unserialize($group['AdminAuth']);
            $group_auth[] = $new['Key'];

            $dbw->update('AdminGroup', ['AdminAuth'=>serialize($group_auth)], ['AdminGroupId'=>$group['AdminGroupId']]);
        }
        else
        {
            //更新
            $re = $dbw->update('AdminMenu', $new, ['AdminMenuId'=>$id]);
            $this->adminLog('更新菜单', $id, 'update', serialize($new));
        }

        if ($re !== false)
        {
            self::output(['title'=>'操作成功','type'=>'success']);
        }
        self::output(['title'=>'操作失败','type'=>'error']);
    }


    public function doDelMenu()
    {
        $id = intval($_GET['id']);

        $dbw = Bridge::pdo(Bridge::DB_ADMIN_W);

        $data = $dbw->query("select count(*) as num from AdminMenu where ParentId={$id}")->fetch();
        if ($data['num'] > 0)
        {
            HDshowMsg('删除失败',['type'=>'error','desc'=>'该菜单下还有其他菜单，不可删除！']);
        }

        $re = $dbw->delete('AdminMenu',['AdminMenuId'=>$id]);
        if ($re > 0)
        {
            $this->adminLog('删除菜单', $id, 'del', $id);
            HDshowMsg('已删除');
        }
        HDshowMsg('删除失败',['type'=>'error']);
    }



}


$app->run();
